package xyz.wbsite.screen;

import cn.hutool.core.io.FileUtil;
import cn.hutool.log.StaticLog;
import cn.hutool.setting.dialect.Props;
import me.friwi.jcefmaven.CefInitializationException;
import me.friwi.jcefmaven.UnsupportedPlatformException;
import xyz.wbsite.screen.client.ShortCutUtil;
import xyz.wbsite.screen.client.WebClient;

import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

public class Starter {

    public void run() throws InterruptedException, UnsupportedPlatformException, CefInitializationException, IOException {
        // 检查配置文件
        File configFile = new File("default.config");
        System.out.println(configFile.getAbsolutePath());
        if (!FileUtil.exist(configFile)) {
            throw new IllegalArgumentException("default.config not exist!");
        }
        // 初始化配置
        Props props = new Props(configFile);
        // 初始化客户端
        WebClient webClient = new WebClient(props);
        // 焦点事件监听
        webClient.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                StaticLog.debug("获得焦点事件");
            }

            @Override
            public void focusLost(FocusEvent e) {
                StaticLog.debug("丢失焦点事件");
            }
        });

        // 添加一个鼠标点击监听
        webClient.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                StaticLog.debug("鼠标点击事件");
            }
        });

        // 调用脚本
        webClient.executeJavaScript("console.log('java 调用 JavaScript')");

        // 创建快捷方式使用
        File targetFile = new File("ScreenClient.exe");
        ShortCutUtil.createShortCutForDesktop(targetFile);
    }

    public static void main(String[] args) throws InterruptedException, UnsupportedPlatformException, CefInitializationException, IOException {
        new Starter().run();
    }
}
